import java.io.IOException;
import java.io.ObjectOutputStream;



public class SenderThread extends Thread{
	ObjectOutputStream objOut;
	SharedMessageQueue sharedMsg;
	PeerData peerData;
	public SenderThread(ObjectOutputStream objOut, SharedMessageQueue sharedMsg, String name, PeerData peerData) {
		super(name);
		this.objOut = objOut;
		this.sharedMsg = sharedMsg;
		this.peerData = peerData;
	}
	public void run(){
		Message msg;
		while(true){
			if(!sharedMsg.isEmpty()){
				try {
					msg = sharedMsg.deQueue();
					objOut.writeObject(msg);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else{
				try {
					//Program terminated when peer is no longer interested in any and no one is interested in peer anymore.
					if(peerData.checkForTermination()){
						break;
					}
					
					synchronized (sharedMsg.dummyObject) {
						sharedMsg.dummyObject.wait(500);
					}
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//wait
			}
		}
	}
}
